package cn.stylefeng.guns.modular.rest;


import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.modular.wordstudy.entity.Favorite;
import cn.stylefeng.guns.modular.wordstudy.entity.Word;
import cn.stylefeng.guns.modular.wordstudy.service.FavoriteService;
import cn.stylefeng.guns.modular.wordstudy.service.WordService;
import cn.stylefeng.guns.sys.core.constant.state.LogSucceed;
import cn.stylefeng.guns.sys.core.constant.state.LogType;
import cn.stylefeng.guns.sys.core.log.factory.LogFactory;
import cn.stylefeng.guns.sys.core.log.factory.LogTaskFactory;
import cn.stylefeng.guns.sys.modular.system.entity.OperationLog;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/python")
public class ApiNeedPythonController extends BaseController {

    @GetMapping(value = "exec")
    public Object executePython(String openId) {
        String pythonPath = "/usr/local/thesis/";
        try {
            String command;
            String command2;

            command = "python37 " + pythonPath + "study_word.py " + openId;
            command2 = "python37 " + pythonPath + "upload_file.py " + openId;


            exec_python(command);
            exec_python(command2);
        } catch (IOException e) {
            // logger.error(e.getMessage());
        } catch (InterruptedException e) {
            //  logger.error(e.getMessage());
        }
        return SUCCESS_TIP;
    }
    private void exec_python(String command) throws IOException, InterruptedException {
        Process proc = Runtime.getRuntime().exec(command);
        //错误流
        BufferedReader error = new BufferedReader(new InputStreamReader(proc.getErrorStream(), "GB2312"));
        String err = null;
        StringBuffer sb = new StringBuffer();
        while ((err = error.readLine()) != null) {
            sb.append(err);
        }
        // 返回执行是否成功，0 代表成功，1 代表失败
        int re = proc.waitFor();
        if (re == 1 || sb.length() > 0) {
//                    OperationLog operationLog = LogFactory.createOperationLog(
//                            LogType.EXCEPTION, -1l, "", null, null, sb.toString(), LogSucceed.FAIL);
//                    operationLogService.save(operationLog);
            //  logger.error(sb.toString());
        } else {
//                    OperationLog operationLog = LogFactory.createOperationLog(
//                            LogType.BUSSINESS, -1l, "python爬虫-征求意见_入库", this.getClass().getName(), "executePython", "", LogSucceed.SUCCESS);
//                    operationLogService.save(operationLog);
            // logger.info("tag=" + tag);
        }
    }

}


